package com.gx.smart.base;

import android.graphics.Color;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewStub;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.LayoutRes;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.view.OnApplyWindowInsetsListener;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.blankj.utilcode.util.BarUtils;
import com.gx.smart.base.loading.LoadingView;

/**
 * 基础activity
 */
public abstract class BaseActivityV2 extends AppCompatActivity {

    protected LoadingView mLoadingInitView;
    protected View mNoDataView;
    protected TextView mTxtTitle;
    protected TextView mLeft;
    protected TextView mRight;
    protected ImageView mRightImage;
    protected RelativeLayout mViewStubContent;
    private ViewStub mViewStubToolbar;
    private ViewStub mViewStubInitLoading;
    private ViewStub mViewStubNoData;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initStatusBar();
        setContentView(R.layout.activity_root2);
        initView();
        initContent();
        initData();
    }

    /**
     * 绑定布局
     *
     * @return
     */
    @LayoutRes
    protected abstract int onBindLayout();

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }


    protected void initStatusBar() {
        BarUtils.setStatusBarColor(this, Color.TRANSPARENT);
        BarUtils.setStatusBarLightMode(this, true);
    }


    protected void initView() {
        View statBar = findViewById(R.id.statusBar);
//        ViewGroup.LayoutParams params = statBar.getLayoutParams();
//        params.height = BarUtils.getStatusBarHeight();
//        statBar.setLayoutParams(params);
        ViewCompat.setOnApplyWindowInsetsListener(statBar, new OnApplyWindowInsetsListener() {
            @Override
            public WindowInsetsCompat onApplyWindowInsets(View v, WindowInsetsCompat insets) {
                RelativeLayout.LayoutParams params1  = (RelativeLayout.LayoutParams) v.getLayoutParams();
                params1.topMargin =WindowInsetsCompat.Type.systemBars();
                return insets;
            }
        });
    }

    protected void initContent() {
        initCommonView();
    }

    public void initCommonView() {
        mViewStubToolbar = findViewById(R.id.view_stub_toolbar);
        mViewStubContent = findViewById(R.id.view_stub_content);
        mViewStubInitLoading = findViewById(R.id.view_stub_init_loading);
        mViewStubNoData = findViewById(R.id.view_stub_nodata);

        if (enableToolbar()) {
            mViewStubToolbar.setLayoutResource(onBindToolbarLayout());
            View viewTooBar = mViewStubToolbar.inflate();
            initTooBar(viewTooBar);
        }
        initContentView(mViewStubContent);
    }

    public void initContentView(ViewGroup root) {
        LayoutInflater.from(this).inflate(onBindLayout(), root, true);
    }

    public int onBindToolbarLayout() {
        return R.layout.layout_common_title;
    }

    public boolean enableToolbar() {
        return true;
    }

    protected void initTooBar(View view) {
        mLeft = view.findViewById(R.id.left);
        mTxtTitle = view.findViewById(R.id.center_title);
        mRight = view.findViewById(R.id.right);
        mRightImage = view.findViewById(R.id.rightImageView);
        if (getLeft()) {
            mLeft.setVisibility(View.VISIBLE);
            mLeft.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onBackPressed();
                }
            });
        }

        if (!TextUtils.isEmpty(getToolbarTitle())) {
            mTxtTitle.setVisibility(View.VISIBLE);
            mTxtTitle.setText(getToolbarTitle());
        }

        if (!TextUtils.isEmpty(getRight())) {
            mRight.setVisibility(View.VISIBLE);
            mRight.setText(getRight());
        }

        if (getRightImage() != -1) {
            mRightImage.setVisibility(View.VISIBLE);
            mRightImage.setImageResource(getRightImage());
        }
    }


    public boolean getLeft() {
        return true;
    }


    public void setLeftVisible() {
        if (mLeft != null) {
            mLeft.setVisibility(View.VISIBLE);
            mLeft.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onBackPressed();
                }
            });
        }
    }

    public String getRight() {
        return "";
    }

    public void setRight(String content) {
        if (mRight != null) {
            if (TextUtils.isEmpty(content)) {
                mRight.setVisibility(View.GONE);
            } else {
                mRight.setText(content);
            }

        }
    }

    public TextView getRightTextView() {
        return this.mRight;
    }

    public int getRightImage() {
        return -1;
    }


    public abstract String getToolbarTitle();

    public void setToolbarTitle(String content) {
        if (mTxtTitle != null) {
            mTxtTitle.setText(content);
        }
    }

    protected void initData() {

    }


    public void showInitLoadView(boolean show) {
        if (mLoadingInitView == null) {
            View view = mViewStubInitLoading.inflate();
            mLoadingInitView = view.findViewById(R.id.loadingView);
        }
        mLoadingInitView.setVisibility(show ? View.VISIBLE : View.GONE);
    }


    public void showNoDataView(boolean show) {
        if (mNoDataView == null) {
            View view = mViewStubNoData.inflate();
            mNoDataView = view.findViewById(R.id.view_no_data);
        }
        mNoDataView.setVisibility(show ? View.VISIBLE : View.GONE);
    }


}